package com.ajavaer.framework.im.easemob.message;

import com.ajavaer.framework.common.tools.StringTools;

import java.util.LinkedHashMap;
import java.util.List;

public class EasemobApiMessage<T> extends EasemobMessage {
    private String action;
    private String application;
    private String path;
    private String uri;
    private String organization;
    private String applicationName;
    private T entities;
    private T data;
    private Long timestamp;
    private Integer duration;

    private LinkedHashMap params;
    /**
     * 游标
     * <p>
     * 分页默认返回按照创建时间排序，如果需要指定获取数量，需加上参数 limit=N，N 为数量值。关于分页：如果 DB 中的数量大于 N，
     * 返回 JSON 会携带一个字段“cursor”,我们把它叫做”游标”，该游标可理解为结果集的指针，值是变化的。
     * 往下取数据的时候带着游标，就可以获取到下一页的值。如果还有下一页，返回值里依然还有这个字段，直到没有这个字段，说明已经到最后一页。
     * cursor的意义在于数据（真）分页。
     * </p>
     */
    private String cursor;
    /**
     * 本次获取到的条数
     */
    private Integer count;

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getApplication() {
        return application;
    }

    public void setApplication(String application) {
        this.application = application;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }

    public String getApplicationName() {
        return applicationName;
    }

    public void setApplicationName(String applicationName) {
        this.applicationName = applicationName;
    }

    public T getEntities() {
        return entities;
    }

    public void setEntities(T entities) {
        this.entities = entities;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public Integer getDuration() {
        return duration;
    }

    public void setDuration(Integer duration) {
        this.duration = duration;
    }

    public String getCursor() {
        if (params != null) {
            Object cursorArray = params.get("cursor");
            if (cursorArray instanceof String[]) {
                this.cursor = ((String[]) cursorArray)[0];
            } else {
                this.cursor = cursorArray.toString();
            }
        }
        return cursor;
    }

    public void setCursor(String cursor) {
        this.cursor = cursor;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Boolean hasNext() {
        return StringTools.isNotBlank(cursor);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public LinkedHashMap getParams() {
        return params;
    }

    public void setParams(LinkedHashMap params) {
        this.params = params;
    }
}
